package com.im.chat.controller;

import com.im.chat.controller.util.AdminDto;
import com.im.chat.controller.util.AdminRedisCache;
import com.im.chat.login.UserDto;
import com.im.chat.service.ChatRecordService;
import com.im.chat.service.ChatSessionService;
import com.im.chat.service.ResourceService;
import com.im.chat.util.cache.redis.RedisCache;
import com.im.chat.util.jsonp.CommonConstant;
import com.im.chat.util.jsonp.Jsonp;
import com.im.chat.util.jsonp.Jsonp_data;
import com.im.chat.websocket.WebSocketServer;
import com.im.chat.websocket.dto.AdminStatusInfo;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * WebSocketController
 * @author zhengkai.blog.csdn.net
 */
@RestController
@RequestMapping("adminStatus")
public class NoLoginController {

    @Autowired
    private ChatSessionService chatSessionService;
    @Autowired
    private ChatRecordService chatRecordService;
    /**
     * 修改管理员在线状态接口
     * @param adminSid
     * @param status   1-在线，2-忙碌，3-离开。
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAdminSession", method = RequestMethod.GET)
    public Object blackSession(
            @RequestParam(name = "adminSid", required = true) String adminSid,
            @RequestParam(name = "status", required = true) int status
    ) {
        if (StringUtils.isEmpty(adminSid)){
            return Jsonp.paramError("adminSid不能为空!");
        }
        AdminDto user = (AdminDto) AdminRedisCache.getAdminDto(adminSid);
        if (ObjectUtils.equals(null, user)){
            return Jsonp.noLoginError(CommonConstant.CommonMessage.NO_LOGIN);
        }
        Long adminId = user.getAdminId();
        ConcurrentHashMap<String, AdminStatusInfo> adminStatusMap=  WebSocketServer.adminStatusMap;
        ConcurrentHashMap<String, WebSocketServer> webSocketMap=  WebSocketServer.webSocketMap;
        AdminStatusInfo adminStatusInfo = adminStatusMap.get("admin"+adminId);
            //return Jsonp_data.success(adminStatusInfo);
        return Jsonp.success();

    }

//    /***
//     * 获取客服列表
//     */
//    @ResponseBody
//    @RequestMapping("getAllAdmins")
//    public Object getAllAdmins(
//            @RequestParam(value="adminSid",defaultValue="")String adminSid){
//        try {
//            if (CheckIsEmpty.isEmpty(adminSid)) {
//                return Jsonp.paramError(CommonMessage.PARAM_ERROR_MESSAGE);
//            }
//            List<AuthUser> authUsers = authUserService.findDistributionUser(2);
//            return Jsonp_data.success(authUsers);
//        } catch (Exception e) {
//            e.printStackTrace();
//            LOGGER.error("AdminAuthLoginController退出登录有误APP" + e);
//            return Jsonp.error();
//        }
//    }


    @RequestMapping("/push/{visitType}/{visitId}")
    public ResponseEntity<String> pushToWeb(@PathVariable String message, @PathVariable String visitType,  @PathVariable String visitId) throws IOException {
        WebSocketServer.sendInfo(message,visitType,visitId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }


    /**
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSocket", method = RequestMethod.GET)
    public Object delMessage(
    ) {
        HashMap<String,Object> datamap = new HashMap<>();
        ConcurrentHashMap<String, AdminStatusInfo> adminStatusMap=  WebSocketServer.adminStatusMap;
        ConcurrentHashMap<String, WebSocketServer> webSocketMap=  WebSocketServer.webSocketMap;
        int onlineUserCount =  WebSocketServer.onlineUserCount;
        int onlineAdminCount  =  WebSocketServer.onlineAdminCount;
        datamap.put("adminStatusMap",adminStatusMap);
        datamap.put("webSocketMap",webSocketMap);
        datamap.put("onlineUserCount",onlineUserCount);
        datamap.put("onlineAdminCount",onlineAdminCount);
        return Jsonp_data.success(datamap);
    }
}
